package com.forg.dubbox.example.consumer.service;


import com.forg.dubbox.example.client.service.IExampleService;
import com.forg.dubbox.example.client.service.ITestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * Created by forgkan on 15.11.9.
 */
@Component("normalConsumerService")
public class NormalConsumerServiceImpl {

    private static final Logger logger = LoggerFactory.getLogger(NormalConsumerServiceImpl.class);

    @Autowired
    @Qualifier("normalProviderServiceImpl")
    IExampleService normalProviderServiceImpl;

    @Autowired
    @Qualifier("testProviderServiceImpl")
    ITestService testProviderServiceImpl;

    public String getStr(Integer times,Integer size){

        if(times == null || times <= 0){
            times = 1;
        }

        if(size == null || size <= 0){
            size = 1;
        }

        logger.info("getStr times={},size={}",times,size);
        long start = System.currentTimeMillis();
        for (int i = 1 ;i<= times; i ++){
            logger.info("invoke getStr {} times,result = {}",i,normalProviderServiceImpl.getStr(size));
        }
        long end = System.currentTimeMillis();

        long interval = (end - start)/1000;

        logger.info("consumer invoke getStr end,cost {} ms", interval);

        return String.format("consumer invoke getStr % 5d times,size % 6d,cost % 6d ms.", Integer.valueOf(times), Integer.valueOf(size * 72), interval);
    }

    public String sayHello(String name){

        return testProviderServiceImpl.sayHello(name);
    }
}
